#include "historymessagewidget.h"

//////////////////////////////
/// 单个历史消息元素
//////////////////////////////
HistoryItem::HistoryItem() {}

HistoryItem *HistoryItem::makeHistoryItem(const MessageInfo &msg)
{
    // 创建出对象
    HistoryItem *item = new HistoryItem();
    item->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);

    // 创建布局管理器
    QGridLayout *layout = new QGridLayout();
    layout->setHorizontalSpacing(5);
    layout->setVerticalSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
    item->setLayout(layout);

    // 创建头像
    QPushButton *avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(40, 40);
    avatarBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    avatarBtn->setIconSize(QSize(40, 40));
    avatarBtn->setIcon(msg._sender._avatar);

    // 创建昵称和时间
    QLabel *nameLabel = new QLabel();
    nameLabel->setText(msg._sender._nickname + "|" + msg._time);
    nameLabel->setFixedHeight(20);
    nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    // 消息内容部分
    QWidget *contentWidget = nullptr;
    if (msg._message_type == model::STRING_TYPE)
    {
        QLabel *msgLabel = new QLabel();
        msgLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        msgLabel->setWordWrap(true);
        msgLabel->setText(QString(msg._content));
        msgLabel->adjustSize();
        contentWidget = msgLabel;
    }
    else if (msg._message_type == model::IMAGE_TYPE)
    {

    }
    else if (msg._message_type == model::FILE_TYPE)
    {

    }
    else if (msg._message_type == model::SPEECH_TYPE)
    {

    }
    else
    {
        ERROR() <<"消息类型错误: " << msg._message_type;
        return nullptr;
    }

    // 添加到布局管理器中
    layout->addWidget(avatarBtn, 0, 0, 2, 1);
    layout->addWidget(nameLabel, 0, 1, 1, 1);
    layout->addWidget(contentWidget, 1, 1, 5, 5);
    return item;
}

//////////////////////////////
/// 搜索历史消息界面
//////////////////////////////
HistoryMessageWidget::HistoryMessageWidget(QWidget *parent)
    :QDialog(parent)
{
    // 设置基本属性
    this->setFixedSize(600, 600);
    this->setWindowTitle("历史消息");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setStyleSheet("QWidget { background-color: rgb(255, 255, 255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 创建布局管理器
    _layout = new QGridLayout();
    _layout->setHorizontalSpacing(5);
    _layout->setVerticalSpacing(5);
    _layout->setContentsMargins(20, 10, 20, 5);
    _layout->setAlignment(Qt::AlignTop);
    this->setLayout(_layout);

    // 创建单选按钮
    _keyRadioBtn = new QRadioButton();
    _timeRadioBtn = new QRadioButton();

    _keyRadioBtn->setText("按关键字查询");
    _timeRadioBtn->setText("按时间查询");

    _keyRadioBtn->setChecked(true); // 默认按关键字搜索

    _layout->addWidget(_keyRadioBtn, 0, 0, 1, 2);
    _layout->addWidget(_timeRadioBtn, 0, 2, 1, 2);

    // 创建搜索框
    _searchEdit = new QLineEdit();
    _searchEdit->setFixedHeight(30);
    _searchEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    _searchEdit->setPlaceholderText("输入关键词");
    _searchEdit->setStyleSheet("QLineEdit { border: none; border-radius: 10px; background-color: rgb(240, 240, 240); font-size: 16px; padding-left: 10px; }");
    _layout->addWidget(_searchEdit, 1, 0, 1, 8);

    // 创建搜索按钮
    _searchBtn = new QPushButton();
    _searchBtn->setFixedSize(30, 30);
    _searchBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    _searchBtn->setIconSize(QSize(25, 25));
    _searchBtn->setIcon(QIcon(":/resource/image/search.png"));
    QString btnStyle = "QPushButton { border: none; background-color: rgb(240, 240, 240); border-radius: 10px; }";
    btnStyle += "QPushButton:pressed { background-color: rgb(220, 220, 220); }";
    _searchBtn->setStyleSheet(btnStyle);
    _layout->addWidget(_searchBtn, 1, 8, 1, 1);

    // 创建时间相关的部分控件
    _begTag = new QLabel();
    _begTag->setText("开始时间");
    _begTag->hide();
    _endTag = new QLabel();
    _endTag->setText("结束时间");
    _endTag->hide();

    _begTimeEdit = new QDateTimeEdit();
    _endTimeEdit = new QDateTimeEdit();
    _begTimeEdit->setFixedHeight(30);
    _endTimeEdit->setFixedHeight(30);
    _begTimeEdit->hide();
    _endTimeEdit->hide();

    // 创建滚动区域
    initScrollArea();

    // 关联槽函数
    connect(_keyRadioBtn, &QRadioButton::clicked, this, [=](){
        _layout->removeWidget(_begTag);
        _layout->removeWidget(_begTimeEdit);
        _layout->removeWidget(_endTag);
        _layout->removeWidget(_endTimeEdit);
        _begTag->hide();
        _begTimeEdit->hide();
        _endTag->hide();
        _endTimeEdit->hide();

        _layout->addWidget(_searchEdit, 1, 0, 1, 8);
        _searchEdit->show();
    });

    connect(_timeRadioBtn, &QRadioButton::clicked, this, [=](){
        _layout->removeWidget(_searchEdit);
        _searchEdit->hide();

        _layout->addWidget(_begTag, 1, 0, 1, 1);
        _layout->addWidget(_begTimeEdit, 1, 1, 1, 3);
        _layout->addWidget(_endTag, 1, 4, 1, 1);
        _layout->addWidget(_endTimeEdit, 1, 5, 1, 3);

        _begTag->show();
        _begTimeEdit->show();
        _endTag->show();
        _endTimeEdit->show();
    });

#if TEST_UI
    for (int i = 0; i < 20; i++)
    {
        model::UserInfo user;
        user._user_id = "123";
        user._avatar = QIcon(":/resource/image/defaultAvatar.png");
        user._nickname = "张三";
        user._desciption = "........";
        user._mail = "231560";
        MessageInfo msg = MessageInfo::makeMessage(model::STRING_TYPE, "1111", user, ("历史消息" + QString::number(i)).toUtf8());
        addHistoryMsg(msg);
    }
#endif
}

void HistoryMessageWidget::initScrollArea()
{
    // 创建滚动区域
    _scrollArea = new QScrollArea();
    _scrollArea->setWidgetResizable(true);
    _scrollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal { height: 0px; }");
    _scrollArea->verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width: 2px; background-color: rgb(255, 255, 255); }");
    _scrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    _scrollArea->setStyleSheet("QWidget { background-color: transparent; border: none; }");

    // 创建widget
    _container = new QWidget();
    _scrollArea->setWidget(_container);

    // 创建布局管理器
    _vlayout = new QVBoxLayout();
    _vlayout->setSpacing(10);
    _vlayout->setContentsMargins(0, 0, 0, 0);
    _vlayout->setAlignment(Qt::AlignTop);
    _container->setLayout(_vlayout);

    _layout->addWidget(_scrollArea, 2, 0, 1, 9);
}

void HistoryMessageWidget::addHistoryMsg(const MessageInfo &msg)
{
    HistoryItem *item = HistoryItem::makeHistoryItem(msg);
    _vlayout->addWidget(item);
}

void HistoryMessageWidget::clear()
{
    for (int i = _vlayout->count() - 1; i >= 0; i--)
    {
        QWidget* item = _vlayout->itemAt(i)->widget();
        if (item == nullptr) continue;
        _vlayout->removeWidget(item);
        item->deleteLater();
    }
}


